﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Dal
{
  public  class DBhelper
    {
        public static string conStr = "server=YZY;database=Logistics;uid=sa;pwd=123";
        public static SqlDataReader ExecuteReader(string sql, CommandType type = CommandType.Text, params SqlParameter[] pams)
        {
            //声明数据库连接
            SqlConnection con = new SqlConnection(conStr);
            //声明命令
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                //添加参数
                if (pams != null && pams.Length > 0)
                {
                    cmd.Parameters.AddRange(pams);
                }
                //打开数据连接
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                //执行存储过程
                cmd.CommandType = type;
                //执行命令
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }


        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sqlstr">执行SQL</param>
        /// <param name="paras">参数</param>
        /// <returns>返回结果</returns>
        public static DataTable GetDataTable(string sqlstr, SqlParameter[] paras)
        {
            //连接对象
            using (SqlConnection conn = new SqlConnection(conter))
            {
                //执行命令
                using (SqlCommand scmd = new SqlCommand(sqlstr, conn))
                {
                    //设置参数
                    if (paras != null && paras.Length > 0)
                    {
                        scmd.Parameters.AddRange(paras);
                    }
                    //创建SQLdataadapter对象
                    SqlDataAdapter sda = new SqlDataAdapter(scmd);
                    //创建datatable对象
                    DataTable dt = new DataTable();
                    //把数据保存到datatable表中
                    sda.Fill(dt);

                    return dt;
                }
            }
        }
        /// <summary>
        /// 增、删、改
        /// </summary>
        /// <param name="sqlstr">执行SQL</param>
        /// <param name="paras">参数</param>
        /// <returns>返回结果</returns>
        public static int ExecuteNonQuery(string sqlstr, SqlParameter[] paras)
        {
            //连接对象
            SqlConnection conn = new SqlConnection(conter);

            //执行命令
            using (SqlCommand scmd = new SqlCommand(sqlstr, conn))
            {
                //设置参数
                if (paras != null && paras.Length > 0)
                {
                    scmd.Parameters.AddRange(paras);
                }
                //打开数据库链接
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                //返回受影响的行数
                int r = scmd.ExecuteNonQuery();
                //关闭数据库链接
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
                //执行命令
                return r;
            }
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="sqlstr">执行sql</param>
        /// <param name="paras">执行参数</param>
        /// <returns>返回结果</returns>
        public static object ExecuteScalar(string sqlstr, SqlParameter[] paras)
        {
            //连接对象
            using (SqlConnection conn = new SqlConnection(conter))
            {
                //执行命令
                using (SqlCommand scmd = new SqlCommand(sqlstr, conn))
                {
                    //设置参数
                    if (paras != null && paras.Length > 0)
                    {
                        scmd.Parameters.AddRange(paras);
                    }
                    //打开数据库链接
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    //关闭数据库链接
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                    //执行命令
                    return scmd.ExecuteScalar();
                }
            }
        }
    }
}
